#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class RolePermissionsProvider : CY.GFive.Core.Providers.DALProvider.IRolePermissionsProvider
	{
		#region Sql
		private static readonly string SqlSelectRolePermissions = "SELECT [Id], [RoleKey],[PermissionsKey] FROM [RolePermissions] WHERE  [Id]=@Id";

		private static readonly string SqlInsertRolePermissions = "INSERT INTO [RolePermissions]([RoleKey],[PermissionsKey]) values(@RoleKey,@PermissionsKey);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateRolePermissions = "UPDATE [RolePermissions] SET [RoleKey] = @RoleKey,[PermissionsKey] = @PermissionsKey WHERE  [Id]=@Id";

		private static readonly string SqlDeleteRolePermissions = "DELETE [RolePermissions] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllRolePermissions = "SELECT [Id], [RoleKey],[PermissionsKey] FROM [RolePermissions]";
		#endregion

		#region IRolePermissionsProvider Members
		public Core.Business.RolePermissions Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectRolePermissions);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.RolePermissions rolePermissions = new Core.Business.RolePermissions();

				if (!reader.IsDBNull(0)) rolePermissions.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) rolePermissions.RoleKey = reader.GetInt32(1);
				if (!reader.IsDBNull(2)) rolePermissions.PermissionsKey = reader.GetInt32(2);

				return rolePermissions;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.RolePermissions rolePermissions)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@RoleKey", SqlDbType.Int, rolePermissions.RoleKey);
			sql.AddParameter("@PermissionsKey", SqlDbType.Int, rolePermissions.PermissionsKey);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertRolePermissions);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) rolePermissions.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.RolePermissions rolePermissions)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@RoleKey", SqlDbType.Int, rolePermissions.RoleKey);
			sql.AddParameter("@PermissionsKey", SqlDbType.Int, rolePermissions.PermissionsKey);
			sql.AddParameter("@Id", SqlDbType.Int, rolePermissions.Id);
			sql.ExecuteSql(SqlUpdateRolePermissions);
		}

		public void Delete(Core.Business.RolePermissions rolePermissions)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, rolePermissions.Id);
			sql.ExecuteSql(SqlDeleteRolePermissions);
		}

		public IList<Core.Business.RolePermissions> GetAllRolePermissions()
		{
			IList<Core.Business.RolePermissions> rolePermissionslist = new List<Core.Business.RolePermissions>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllRolePermissions);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.RolePermissions rolePermissions = new Core.Business.RolePermissions();

					if (!reader.IsDBNull(0)) rolePermissions.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) rolePermissions.RoleKey = reader.GetInt32(1);
					if (!reader.IsDBNull(2)) rolePermissions.PermissionsKey = reader.GetInt32(2);

					rolePermissions.MarkOld();
					rolePermissionslist.Add(rolePermissions);
				}
				reader.Close();
			}
			return rolePermissionslist;
		}
		#endregion
	}
}
